# -*- coding: utf-8 -*-
'''
Created on 2013-4-23

@author: dyq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms.plus import  page,user
from Models import  models

@page.page_render(user.Admin())
def Index(request):
        '''
                            列表页
        '''
        list= models.Article_Type.objects.filter().order_by("Path") #倒序加上-号
    
        return Find.View(request,list)

@page.page_render(user.Admin())
def Tree(request):
       
        return Find.View(request)

@page.page_render(user.Admin())
def DoTree(request):
        '''
                            列表页
        '''
        list= models.Article_Type.objects.filter().order_by("Path") #倒序加上-号
        treehash={}
        treelist=range(len(list))
        i=1
        treelistcount=0
        for info in list:
            pid=int(treehash.get(info.SuperiorId,"0"))
            treelist[treelistcount]={'id':i, 'pId':pid, 'name': info.Title,'val':info.ArticleTypeid}
            treehash[info.ArticleTypeid]=i
            i=i+1
            treelistcount=treelistcount+1
    
        return Find.Json(treelist)

    
@page.page_render(user.Admin())
def Add(request):
        '''
                           添加
        '''
        listTemplate=models.Article_Template.objects.filter()
        SuperiorId = request.GET.get("SuperiorId", None)
        Superior=None
        if SuperiorId==None:
            Superior= models.Article_Type(ArticleTypeid="-1",Title="顶级")
        else:
            Superior= models.Article_Type.objects.get(ArticleTypeid=SuperiorId)
            
        return Find.View(request,{"tmpe":listTemplate,"Superior":Superior})
    
@page.page_render(user.Admin())
def DoAdd(request):
        '''
                            添加
        '''
        
        SuperiorId=request.POST.get("SuperiorId", None)
        templateId=int(request.POST.get("TemplateId"))
        
        infoSuperior=None
        if SuperiorId and SuperiorId!="-1":
            infoSuperior=models.Article_Type.objects.get(ArticleTypeid=SuperiorId)
        else:
            infoSuperior=models.Article_Type(ArticleTypeid="-1",Path="/")
        
        ArticleTypeid=tool.guid()
        info = models.Article_Type(ArticleTypeid=ArticleTypeid,
                                   Title=request.POST.get("Title"),
                                 TemplateId=models.Article_Template.objects.get(id=templateId),
                                 IsLock=0,
                                 SuperiorId=infoSuperior.ArticleTypeid,
                                 Path=infoSuperior.Path+ArticleTypeid+"/")
        
        Find.UpdateModel(request,info)
        return Find.JsonX(False, "操作成功")
    

@page.page_render(user.Admin())
def Edit(request):
        '''
                            查看
        '''
        listTemplate=models.Article_Template.objects.filter()
        info=models.Article_Type.objects.get(ArticleTypeid=request.GET.get("id"))
        return Find.View(request,{"info":info,"temp":listTemplate})
    
@page.page_render(user.Admin())
def DoEdit(request):
        '''
                            查看
        '''
        info=models.Article_Type.objects.get(ArticleTypeid=request.POST.get("id"))
        info.Title=request.POST.get("Title")
        info.TemplateId=models.Article_Template.objects.get(id=request.POST.get("TemplateId"))
        info.save()
        return Find.JsonX(False, "操作成功")
    
@page.page_render(user.Admin())
def DoDel(request):
        '''
                            删除
        '''
        info=models.Article_Type.objects.get(ArticleTypeid=request.GET.get("Id"))
        count=len(models.Article_Content.objects.filter(ArticleTypeid=info))
        typecount=len(models.Article_Type.objects.filter(SuperiorId=info.ArticleTypeid))
        if count==0 and typecount==0:
            info.delete()
            return Find.JsonX(False, "操作成功")
        else:
            return Find.JsonX(True, "这个分类已经被使用，不能删除")
        
    
@page.page_render(user.Admin())
def Mobile(request):
        '''
                            移动
        '''
        list= models.Article_Type.objects.filter(id!=request.Get.get("Id")).order_by("Path") 
        info=models.Article_Type.objects.get(Id=request.Get.get("fromId")) 
       
        return Find.View(request,{"info":info,"list":list})
    
@page.page_render(user.Admin())
def DoMobile(request):
        '''
                            移动到
        '''
        dataOrderList=request.POST.get("dataOrderid").split(',')
        
        #for key in dataOrderList:
            
        
        
        
    
        return Find.JsonX(False, "操作成功")

    
    